/**
 * 
 */
package com.yst.metho.web.application.projectversion;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

import javax.ejb.CreateException;
import javax.ejb.FinderException;
import javax.naming.NamingException;

import nextapp.echo2.app.FillImage;
import nextapp.echo2.extras.app.TabPane;
import nextapp.echo2.extras.app.layout.TabPaneLayoutData;

import com.yst.metho.web.application.components.ListingViewPane;
import com.yst.metho.web.application.projectversion.actions.lot.DeleteLotAction;
import com.yst.metho.web.application.projectversion.actions.version.ClosePrjVersionAction;
import com.yst.metho.web.application.projectversion.actions.version.OpenPrjVersionAction;
import com.yst.metho.web.application.projectversion.actions.wbs.DeleteActivityAction;
import com.yst.metho.web.application.projectversion.renderer.ProjectClassificationTableRenderer;
import com.yst.metho.web.application.projectversion.renderer.ProjectLotTableRenderer;
import com.yst.metho.web.application.projectversion.renderer.ProjectVersionTableRenderer;
import com.yst.metho.web.application.projectversion.renderer.ProjectWBSTableRenderer;
import com.yst.metho.web.application.projectversion.views.LotListingViewPane;
import com.yst.metho.web.application.projectversion.views.ProjectClassificationListModel;
import com.yst.metho.web.application.projectversion.views.ProjectLotListModel;
import com.yst.metho.web.application.projectversion.views.ProjectVersionListModel;
import com.yst.metho.web.application.projectversion.views.ProjectVersionTableModel;
import com.yst.metho.web.application.projectversion.views.ProjectWBSListModel;
import com.yst.metho.web.application.projectversion.views.ProjectWBSListingViewPane;
import com.yst.metho.web.application.utils.ExceptionMgt;
import com.yst.metho.web.application.utils.Locator;
import com.yst.metho.web.application.utils.Styles;
import com.yst.suivi.data.project.Project;

/**
 * Vue qui permet d'afficher le detail d'un projet.
 * 
 * @author perso
 * 
 */
public class ProjectDetailTabbedPane extends TabPane
{
	public static final FillImage[] TAB_FILL_IMAGES = new FillImage[]
	{ null, Styles.FILL_IMAGE_SHADOW_BACKGROUND_DARK_BLUE, Styles.FILL_IMAGE_SHADOW_BACKGROUND_LIGHT_BLUE, Styles.FILL_IMAGE_PEWTER_LINE, Styles.FILL_IMAGE_LIGHT_BLUE_LINE, Styles.FILL_IMAGE_SILVER_LINE };

	private Project mPrj;

	public ProjectDetailTabbedPane(Project prj)
	{
		mPrj = prj;
		ListingViewPane listingPane;
		try
		{
			for (int index = 0; index < TAB_FILL_IMAGES.length; index++)
			{
				setTestComponentProperty(TabPane.PROPERTY_TAB_ACTIVE_BACKGROUND_IMAGE, FillImage.class, TAB_FILL_IMAGES[index]);
				setTestComponentProperty(TabPane.PROPERTY_TAB_INACTIVE_BACKGROUND_IMAGE, FillImage.class, TAB_FILL_IMAGES[index]);
			}

			TabPaneLayoutData layoutData = new TabPaneLayoutData();
			layoutData.setTitle("Version");
			ProjectVersionTableModel prjVersionTableModel = new ProjectVersionTableModel(mPrj);
			listingPane = new ListingViewPane(prjVersionTableModel, new ProjectVersionTableRenderer(), true, false, null, true, new ClosePrjVersionAction(prjVersionTableModel, mPrj), true, new OpenPrjVersionAction(prjVersionTableModel, mPrj));
			listingPane.setLayoutData(layoutData);
			add(listingPane);

			ProjectVersionListModel prjVersionListModel = new ProjectVersionListModel(Locator.getProjectVersionView().findProjectVersion(prj.getId()));
			prjVersionTableModel.addChangeListener(prjVersionListModel);

			ProjectLotListModel lotListModel = new ProjectLotListModel(prjVersionListModel, mPrj);
			LotListingViewPane lotView = new LotListingViewPane(lotListModel, new ProjectLotTableRenderer(), true, true, new DeleteLotAction(lotListModel, lotListModel.getPrj()));
			layoutData = new TabPaneLayoutData();
			layoutData.setTitle("Lot");
			lotView.setLayoutData(layoutData);
			add(lotView);

			ProjectWBSListModel wbsListModel = new ProjectWBSListModel(prjVersionListModel, mPrj);
			ProjectWBSListingViewPane wbsView = new ProjectWBSListingViewPane(wbsListModel, new ProjectWBSTableRenderer(), true, true, new DeleteActivityAction(wbsListModel, wbsListModel.getPrj()));
			layoutData = new TabPaneLayoutData();
			layoutData.setTitle("WBS");
			wbsView.setLayoutData(layoutData);
			add(wbsView);

			listingPane = new ListingViewPane(new ProjectClassificationListModel(mPrj), new ProjectClassificationTableRenderer());
			layoutData = new TabPaneLayoutData();
			layoutData.setTitle("Classification");
			listingPane.setLayoutData(layoutData);
			add(listingPane);
		}
		catch (NamingException e)
		{
			ExceptionMgt.manage(e);
		}
		catch (FinderException e)
		{
			ExceptionMgt.manage(e);
		}
		catch (CreateException e)
		{
			ExceptionMgt.manage(e);
		}
	}

	private void setTestComponentProperty(String propertyName, Class propertyClass, Object newValue)
	{
		try
		{
			String methodName = "set" + Character.toUpperCase(propertyName.charAt(0)) + propertyName.substring(1);
			Method setPropertyMethod = this.getClass().getMethod(methodName, new Class[]
			{ propertyClass });
			setPropertyMethod.invoke(this, new Object[]
			{ newValue });
		}
		catch (NoSuchMethodException ex)
		{
			ExceptionMgt.manage(ex);
		}
		catch (IllegalArgumentException ex)
		{
			ExceptionMgt.manage(ex);
		}
		catch (IllegalAccessException ex)
		{
			ExceptionMgt.manage(ex);
		}
		catch (InvocationTargetException ex)
		{
			ExceptionMgt.manage(ex);
		}
	}
}
